package com.sto.stoapp.activity.scanner.inquiry;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.sto.stoapp.activity.R;
import com.sto.stoapp.broadcast.HomeBroadAction;
import com.sto.stoapp.manager.SoundManager;
import com.sto.stoapp.status.EnumTableNames;
import com.sto.stoapp.status.OperateEnumTable;
import com.sto.stoapp.thread.BaseThread;
import com.sto.stoapp.util.Helper;
import com.sto.stoapp.widget.StoHhmEditText;
import com.sto.stoapp.widget.StoListenerActivity;

public class FastInquiryActivity extends StoListenerActivity {
	InquireThread inquireThread;
	private static final int GATHER_DATAS = 3;
	private List<Info> mInfos = new ArrayList<FastInquiryActivity.Info>();;
	private ListView listView;
	private InfoAdapter adapter;
	private StoHhmEditText codev;
	String action;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fast_inquire_layout);
		action = getIntent().getStringExtra(HomeBroadAction.BROAD_TYPE);
		this.setTitleBottom(R.string.fast_search_code, action);
		initView();
		initListener();

	}

	private void initListener() {

		codev.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startFind(codev.getText().toString());

			}
		});
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				showDetails(mInfos.get(position).bus);

			}
		});
	}

	@SuppressLint("ResourceAsColor")
	private void showDetails(String[] details) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		ListView detailsV = new ListView(this);
		detailsV.setBackgroundColor(R.color.white);
		ArrayAdapter<String> detailAdapter = new ArrayAdapter<String>(this,
				R.layout.details_item, R.id.details_item, details);
		detailsV.setAdapter(detailAdapter);
		builder.setView(detailsV);
		builder.create().show();
	}

	private void initView() {
		listView = (ListView) findViewById(R.id.list);
		codev = (StoHhmEditText) findViewById(R.id.code);
	}

	private void startFind(String code) {
		if (code == null || code.length() == 0) {
			Helper.showSoundToast(this, "不能为空", false);
			return;
		}
		doInquire(code);
	}

	Handler mHandler = new Handler() {

		@SuppressWarnings("unchecked")
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case GATHER_DATAS:
				mInfos = (List<FastInquiryActivity.Info>) msg.obj;
				if (mInfos.size() == 0)
					Helper.showSoundToast(FastInquiryActivity.this, "未查到数据",
							false);
				else {
					adapter = new InfoAdapter(FastInquiryActivity.this, mInfos);
					listView.setAdapter(adapter);
				}
			default:
				break;
			}
		}
	};

	private void doInquire(String code) {
		if (mInfos != null) {
			mInfos.clear();
		}
		inquireThread = new InquireThread(this, mHandler, code);
		inquireThread.start();

	}


	class InquireThread extends BaseThread {
		private Context mc;
		private String code;
		Handler handler;

		public InquireThread(Context c, Handler handler, String code) {
			super(c);
			this.code = code;
			this.mc = c;
			this.handler = handler;
			// showDialog();
			showDialogByMessage("正在查询");
		}

		@Override
		public void run() {
			super.run();
			List<EnumTableNames> tableNames = null;
			if (HomeBroadAction.AVIATION_BROAD.equals(action)) {
				tableNames = OperateEnumTable.getAviationTables();
			}
			else if (HomeBroadAction.CENTER_BROAD.equals(action)) {
				tableNames = OperateEnumTable.getCenterTables();
			}
			else if (HomeBroadAction.SCANNER_MAN_BROAD.equals(action)) {
				tableNames = OperateEnumTable.getScannerTables();
			}
			else if (HomeBroadAction.SALES_MAN_BROAD.equals(action)) {
				tableNames = OperateEnumTable.getSaleTables();
			}
			else {
				tableNames = OperateEnumTable.getAllTables();
			}
			List<Info> infos = new ArrayList<FastInquiryActivity.Info>();
			for (EnumTableNames enumc : tableNames) {
				if (!"".equals(enumc.getTableName())) {

					String tableName = enumc.getTableName();
					if (tableName != null) {

						InquiryQueryTable manger = new InquiryQueryTable(mc);
						String[] bus = manger.getCodeInquiryDetail(tableName,
								code, enumc.ordinal());

						if (bus != null) {
							Info info = new Info();
							info.state = bus[12];
							info.bus = bus;
							info.code = code;
							info.name = getString(Integer.parseInt(enumc
									.getScanTypeName()));
							infos.add(info);
						}

					}
				}
			}
			Message msg = handler.obtainMessage();
			msg.what = GATHER_DATAS;
			msg.obj = infos;
			mInfos = infos;
			handler.sendMessage(msg);
			cancelDialog();
		}

	}

	class Info {
		private String code;
		private String name;
		private String state;
		private String[] bus;

		public String getCode() {
			return code;
		}

		public void setCode(String code) {
			this.code = code;
		}

		public String getName() {
			return name;
		}

		public String getState() {
			return state;
		}

		public void setState(String state) {
			this.state = state;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String[] getBus() {
			return bus;
		}

		public void setBus(String[] bus) {
			this.bus = bus;
		}
	}

	class InfoAdapter extends BaseAdapter {

		private Context c;
		private List<Info> infos;

		public InfoAdapter(Context c, List<Info> infos) {
			this.c = c;
			this.infos = infos;
		}

		@Override
		public int getCount() {
			return infos.size();
		}

		@Override
		public Object getItem(int position) {
			return infos.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View view, ViewGroup parent) {
			Holder holder;
			if (view == null) {
				holder = new Holder();
				view = LayoutInflater.from(c).inflate(
						R.layout.fast_search_item, null);
				holder.nameV = (TextView) view.findViewById(R.id.name);
				holder.codeV = (TextView) view.findViewById(R.id.code);
				holder.stateV = (TextView) view.findViewById(R.id.state);
				view.setTag(holder);
			} else {
				holder = (Holder) view.getTag();
			}

			holder.nameV.setText(infos.get(position).getName());
			holder.codeV.setText(infos.get(position).getCode());
			holder.stateV.setText(infos.get(position).getState());
			return view;
		}

	}

	static class Holder {
		TextView nameV;
		TextView codeV;
		TextView stateV;
	}

	@Override
	protected void handlerScanData(String scanData) {
		codev.setText(scanData);
		codev.selectAll();
		startFind(scanData);
		SoundManager.getInstance(FastInquiryActivity.this)
				.playSuccessSound();
		
	}
}
